package Messages;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.concurrent.Phaser;

import com.mysql.jdbc.ResultSet;

import Server.SQLConnection;
import ocsf.server.ConnectionToClient;
/**
 * This class implements the Add Coating to db
 *   @author Shlomit Yosfan
 * @author Nofar Sutliman
 */
public class MessageAddCoating {

	private ConnectionToClient client ;
	private String[] str; 
	static int row1;
	
	/**
	 * Contractor of this class 
	 * @param str
	 * @param client
	 */
	public MessageAddCoating(String[] str, ConnectionToClient client) {
		this.client= client ;
		this.str= str; 
	}
	/**
	 * Adding  Coating to the DB	
	 * @throws SQLException
	 * @throws IOException
	 */
	

	
	public void AddCoating() throws SQLException, IOException{
		
		SQLConnection sc = new SQLConnection();
		
		java.sql.PreparedStatement coatingId = sc.getConnection().prepareStatement("SELECT * FROM db.coating;");
		java.sql.PreparedStatement coatingInsert = sc.getConnection().prepareStatement("INSERT INTO db.coating (CoatingID,CoatingColor,Quantity,MinInventory,MaxInventory,SuppID) VALUES(?,?,?,?,?,?);");
		
		
		ResultSet rs= (ResultSet) coatingId.executeQuery();
		row1 = 0;
		while(rs.next()){
			int temp = rs.getInt(1);
				if(row1>temp)
					row1=row1;
					else{
					row1=temp;}

		}
		
	
		coatingInsert.setInt(1, row1+1);					
		coatingInsert.setString(2,str[1]);
		coatingInsert.setString(3,str[2]);
		coatingInsert.setString(4,str[3]);
		coatingInsert.setString(5, str[4]);
		coatingInsert.setString(6, str[5]);


		coatingInsert.executeUpdate();
		coatingInsert.close();
	
		
							ArrayList<Object> coating=new ArrayList<Object>();
							coating.add("AddCoating");		
							client.sendToClient(coating);
		
		
		
		
	}
				
}
